﻿using EM.FrameWork;
using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class NetFolderController : BaseAuthController
    {
         /// <summary>
        /// 返回Index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="rows">每页条数</param>
        /// <returns></returns>
        public JsonResult GetList(string page, string rows)
        {
            List<object> result = new List<object>();
            NetFolderBLL bll = new NetFolderBLL();
            PagedList<EMOA_NetFolder> list = bll.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows));
            list.ForEach(p =>
            {
                if ( p.NetFolderParentID == new Guid())
                {
                    result.Add(new { NetFolderID = p.NetFolderID, NetFloderName = p.NetFloderName, NetFolderPath = p.NetFolderPath, PowerUserIDs = p.PowerUserIDs, PowerUserNames = PowerUserNames(p.PowerUserIDs), PowerRoleIDs = p.PowerRoleIDs, PowerDepartmentIDs = p.PowerDepartmentIDs });
                }
                else
                {
                    result.Add(new { NetFolderID = p.NetFolderID, NetFloderName = p.NetFloderName, NetFolderPath = p.NetFolderPath, PowerUserIDs = p.PowerUserIDs, PowerUserNames = PowerUserNames(p.PowerUserIDs), PowerRoleIDs = p.PowerRoleIDs, PowerDepartmentIDs = p.PowerDepartmentIDs, NetFolderParentID = p.NetFolderParentID, _parentId = p.NetFolderParentID });
                }
                //result.Add(new NetFolderModels().ToModel(p));
            });

            return ToJson(result, list.TotalItemCount);
        }
        /// <summary>
        /// 使用人
        /// </summary>
        /// <param name="strPowerUserIDs"></param>
        /// <returns></returns>
        public string PowerUserNames(string strPowerUserIDs)
        {
            string result = "";
            if (strPowerUserIDs.ToString().IsNotEmpty())
            {
                string[] ids = strPowerUserIDs.ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                UserBLL bll = new UserBLL();
                List<EMOA_User> entitys = bll.GetList(p => ids.Contains(p.UserID.ToString()));
                entitys.ForEach(p =>
                {
                    result += p.EmployeeName + ";";
                });
            }
            return result;
        }
        public ActionResult GetTreeList()
        {
            NetFolderBLL bll = new NetFolderBLL();
            List<EMOA_NetFolder> list = bll.GetList();
            List<TreeNode> nodeList = new List<TreeNode>();
            list.ForEach(p =>
            {
                nodeList.Add(new TreeNode(p.NetFolderID, p.NetFolderParentID, p.NetFloderName));
            });

            TreeNodeHelper helper = new TreeNodeHelper();
            TreeNode treeNode = new TreeNode();
            treeNode = helper.GenerateTreeRoot(nodeList);

            string result = treeNode.ToJsonTreeString();
            return Content(result);
        }

        public ActionResult GetEntity(Guid id)
        {
            NetFolderBLL bll = new NetFolderBLL();
            var data = bll.GetEntity(id); ;
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 增加实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(NetFolderModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                    NetFolderBLL bll = new NetFolderBLL();
                    EMOA_NetFolder entity = model.ToEntity<EMOA_NetFolder>();
                    entity.NetFolderID = Guid.NewGuid();
                    entity.CreateBy = CurrentUser.UserID;
                    entity.CreateTime = DateTime.Now;                    
		if (bll.Add(entity))
                    {
                        return Content("OK");
                    }
                    else
                    {
                        return Content("增加失败");
                    }
                //}
                //else
                //    return Content("验证失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 修改实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(NetFolderModels model)
        {
            try
            {
                //if (ModelState.IsValid)
                //{
                    NetFolderBLL bll = new NetFolderBLL();
                    EMOA_NetFolder entity = bll.GetEntity(model.NetFolderID);
                    entity = model.ToEntity<EMOA_NetFolder>(entity);
                    entity.UpdateBy = CurrentUser.UserID;
                    entity.UpdateTime = DateTime.Now;                    
		if (bll.Update(entity))
                        return Content("OK");
                    else
                        return Content("修改失败");
                //}
                //else
                //{
                //    return Content("验证失败");
                //}
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                NetFolderBLL bll = new NetFolderBLL();
                if (bll.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("删除失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}